/**
* @file make_stats.c
* @author Jan Sliacky
* 
* Make global statistics from local statistics
*
* April 2010
*
* Copyright (c) 2010 by Jan Sliacky
* All rights reserved.
*/

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/time.h>
#include "globals.h"

static int nProcs;
static int nTasks;
static float testFreq;
static float muTasks;
static float sigTasks;
static float muFallDur;
static float sigFallDur;
static float muFallProb;
static float sigFallProb;
static char *loglvl;

//static struct timeval startTotalTime;
//static struct timeval startWasteTime;
//static struct timeval startFallTimeGlobal;
//static struct timeval startNotWasteTime;

extern char *optarg;
extern int optopt;

static void print_usage(char *program2) {
    printf("Usage: %s -i <input file> \n", program2);
}

int readInputFile2(char *input_file) {
    FILE* file = NULL;
    int ipom = 0;
    float fpom1 = 0;
    float fpom2 = 0;
    char *spom;
    char c[100];

    /* open source file*/
    file = fopen(input_file, "r");
    if (file == NULL) {
        printf("input file has probably wrong path\n\n");
        return(1);
    }

    fgets(c, 100, file);
    sscanf(c,"LOG_LEVEL=%s\n", spom);
    if (loglvl == NULL) {
        loglvl = spom;
    }
    fscanf(file,"NR_PROCS=%d\n", &ipom);
    if (nProcs == 0) {
        nProcs = ipom;
    }
    fscanf(file,"TEST_FREQ=%f\n", &fpom1);
    if (testFreq == 0) {
        testFreq = fpom1;
    }
    fscanf(file,"NR_TASKS=%d\n", &ipom);
    if (nTasks == 0) {
        nTasks = ipom;
    }
    fscanf(file, "MU_TASKS=%f SIG_TASKS=%f\n", &fpom1, &fpom2);
    if (muTasks == 0) {
        muTasks = fpom1;
    }
    if (sigTasks == 0) {
        sigTasks = fpom2;
    }
    fscanf(file, "MU_FALLS_PROB=%f SIG_FALLS_PROB=%f\n", &fpom1, &fpom2);
    if (muFallProb == 0) {
        muFallProb = fpom1;
    }
    if (sigFallProb == 0) {
        sigFallProb = fpom2;
    }
    fscanf(file, "MU_FALLS_DUR=%f SIG_FALLS_DUR=%f\n", &fpom1, &fpom2);
    if (muFallDur == 0) {
        muFallDur = fpom1;
    }
    if (sigFallDur == 0) {
        sigFallDur = fpom2;
    }

    fclose(file);
    return(0);
}

int readGlobalStatFile(char *input_file) {
	//
}

void make_pdf(char *texfile) {
	int iii;
    char str1[] = "pdflatex ";
    char *concatstr;

    concatstr = (char *)calloc(strlen(str1) + strlen(texfile) + 1, sizeof(char));
    strcat(concatstr, str1);
    strcat(concatstr, texfile);
	system(concatstr);
	free(concatstr);
}

int make_tex(char *filename) {
	FILE* file = NULL;

	file = fopen(filename, "w");
	if (file == NULL) {
		printf("error in creating ststistics file\n");
		return(1);
	}
	fprintf(file, "\\documentclass{article}");
	fprintf(file, "\\begin{document}\n");
	fprintf(file, "\\begin{center}\n");
	fprintf(file, "\\begin{tabular}{| c | c | c | c | c | c | c |}\n");
	fprintf(file, "\\hline\n");
	fprintf(file, "zrútení & 6 & 6 & 14 & 31 & 48 & 68  \\\\ \\hline\n");
	fprintf(file, "\\end{tabular}\n");
	fprintf(file, "\\end{center}\n");
	fprintf(file, "\\end{document}\n");
	fclose(file);
	return(0);
}

void make_graph(char *datfile) {
	FILE *pipe = popen("gnuplot -persist","w");
	fprintf(pipe, "set data style lines\n");
	fprintf(pipe, "plot '%s' using 1:2\n", datfile);
	close(pipe);
}

void make_gnuplot_dat(void) {
	//
}

int generate_statistics(int argc, char *argv[]) {
    int c, i;
    char *input_file = NULL;
    char filename[20];

    if (argc < 3) {
        print_usage(argv[0]);
        exit(1);
    }
    while ((c = getopt (argc, argv, "i:")) != -1)
        switch (c) {
        case 'i':
            input_file = optarg;
            break;
        case ':':
            printf("error: option %c needs a value\n\n", optopt);
            print_usage(argv[0]);
            return(1);
            break;
        case '?':
            printf("error: unknown option character\n\n");
            print_usage(argv[0]);
            return(1);
            break;
        }
    readInputFile(input_file);
    for (i = 0; i < nProcs; i++) {
        sprintf(filename, "%s_%d", input_file, i);
        readStatFile(filename);
    }
    sprintf(filename, "%s_all", input_file);
    saveLocalStatistics(filename);
    printf("OK\n");
    return(0);
}
